Home:ALL Converter>Is it possible to get the lagged value in MongoDB?

Is it possible to get the lagged value in MongoDB?

Ask Time:2018-04-27T22:09:17         Author:Mitchell

Json Formatter

I am trying to do a comparison between documents in a MongoDB collection that has a time component. Let's assume the documents look like so:

{date: 2010-01-01, sales: 100}
{date: 2010-01-02, sales: 202}
{date: 2010-01-03, sales: 149}

Is it possible to lag the values for comparison? Is it possible to do some sort of running difference? Perhaps some projection allows for this result:

{date: 2010-01-01, sales: 100, previous_sales: Nan}
{date: 2010-01-02, sales: 202, previous_sales: 100}
{date: 2010-01-03, sales: 149, previous_sales: 202}

Author:Mitchell,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50064196/is-it-possible-to-get-the-lagged-value-in-mongodb
yy